Helpful Information
 
 
Category: XHTML + XSLT
XHTML + XSLT => TXT : what about links ?

Hi

I'm training to transform XHTML to text with a xsl transformation.
However, I haven't found :chomp: the way to transform this :

<SPAN>this is a text with an <A href="http://www.devshed.com">embedded link</A> in the middle.
</SPAN>

to this :

this is a text with an embedded link (http://www.devshed.com) in the middle

All I can do is put the link at the end of the text, which is not really convenient when several links are in the text.

Can anyone help ? Thanks :tntworth:

Xavier

? plain text should have no links.
Do you mean XHTML + XSLT => XHTML (mostly text?)
You probably want to use <xsl:apply-templates>


<xsl:template match="span">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="a">
<xsl:copy-of select="."/>
</xsl:template>

I poorly used the word "link", instead of "url", sorry.

Instead of "All I can do is put the link at the end of the text", please read "All I can do is put the URL at the end of the text"

However the example is accurate.

The text I want to obtain is

"this is a text with an embedded link (http://www.devshed.com) in the middle"

and all I managed to get so far is :

"this is a text with an embedded link in the middle (http://www.devshed.com)"

In the text, I want the URL to be just after the text the link was on ("embedded link") in the XHTML.

I hope this is more understandable.
But thanks for your try.

Xavier

<xsl:template match="span">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="a">
<xsl:value-of select="@href"/>
</xsl:template>

No, all this does is getting the URL.


I don't think you got my point, so here is a test case :
That's the XML (the actual is much more complicated !)



<?xml version="1.0" encoding="UTF-8"?>
<newsletter>
<element>
<span class="texte-dispo">this is some text with a <a href="http://www.google.com" target="_new">link to google</a> and then more text.</span>
</element>
</newsletter>


That's the XSL :


<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso8859-1" indent="no" />

<xsl:template match="newsletter">
Begining
<xsl:apply-templates/>
End
</xsl:template>

<xsl:template match="element">
<xsl:value-of select=".//span[@class='texte-dispo']|.//span[@class='texteColGauche-dispo']"/><xsl:apply-templates select=".//a"/>
</xsl:template>

<xsl:template match="a">
<xsl:text> (</xsl:text><xsl:value-of select="@href"/>)
</xsl:template>

</xsl:stylesheet>


When you run this, it outputs :
Begining
this is some text with a link to google and then more text. (http://www.google.com)

End

What I want to get is :
Begining
this is some text with a link to google (http://www.google.com) and then more text.

End

And I could not find a way to do that...

Thanks
Xavier

No, all this does is getting the URL.


I don't think you got my point, so here is a test case :
That's the XML (the actual is much more complicated !)



<?xml version="1.0" encoding="UTF-8"?>
<newsletter>
<element>
<span class="texte-dispo">this is some text with a <a href="http://www.google.com" target="_new">link to google</a> and then more text.</span>
</element>
</newsletter>


That's the XSL :


<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso8859-1" indent="no" />

<xsl:template match="newsletter">
Begining
<xsl:apply-templates/>
End
</xsl:template>

<xsl:template match="element">
<xsl:value-of select=".//span[@class='texte-dispo']|.//span[@class='texteColGauche-dispo']"/><xsl:apply-templates select=".//a"/>
</xsl:template>

<xsl:template match="a">
<xsl:text> (</xsl:text><xsl:value-of select="@href"/>)
</xsl:template>

</xsl:stylesheet>


When you run this, it outputs :
Begining
this is some text with a link to google and then more text. (http://www.google.com)

End

What I want to get is :
Begining
this is some text with a link to google (http://www.google.com) and then more text.

End

And I could not find a way to do that...

Thanks
Xavier

Hi! Xavier

Try the code provided below.

[code]

<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:template match="newsletter">
Begining
<xsl:apply-templates/>
End
</xsl:template>

<xsl:template match="span[@class='texte-dispo' or @class='texteColGauche-dispo']">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="a">
<xsl:apply-templates/> (<xsl:value-of select="@href"/>)
</xsl:template>

</xsl:stylesheet>

I hope it answers your question.

Cheers,

Sandeep
Innodata-Isogen

P.S. If I proved to be of some help please boost my reputation.

You're calling in that order in your elements template, first taking the value of the span and then the link.
Using <xsl:apply-templates/> inside a span class is the key to your problems.

<xsl:template match="element">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="span[@class='texte-dispo' or @class='texteColGauche-dispo']">
<xsl:apply-templates/>
</xsl:template>

Hi! Xavier

I hope it answers your question.

Cheers,

Sandeep
Innodata-Isogen

P.S. If I proved to be of some help please boost my reputation.

Almost ! Thanks !
I said almost because it does insert a return just after the URL:
Begining
this is some text with a link to google (http://www.google.com)
and then more text.
End

To solve this, I used :


<xsl:template match="a">
<xsl:apply-templates/> (<xsl:value-of select="@href"/>)</xsl:template>


Thanks a lot :-) I guess you can tell this is my first encounter with xslt...

Xavier

Hi Xavier,

You are welcome my friend, am glad, I could be of some help to you.

One has to start somewhere. If you wanna look more in XSL stuff why dont you try w3schools (http://www.w3schools.com/), thats where I started.

Cheers,

Sandeep
Innodata-Isogen

P.S. Hope I've been some help! If so, please boost my reputation!










privacy (GDPR)